<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class UController extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/extlayout';
//	public $layout='//layouts/extlayout';

    public $breadcrumbs = null;

        /**
	 * Returns the static model of the specified AR class.
	 * @var AjaxR
	 */
        public   $R = null;

        public function init()
        {
            $this->R = new AjaxR();
        }

	/**
	*	 取得传递的参数，为测试方便，于是get,post的参数一并获取
	*/
	public function getParam($p, $method = null)
	{

		if (isset($_POST) && isset($_POST[$p])){
			return $_POST[$p];
		}

		if (isset($_GET) && isset($_GET[$p])){
			return $_GET[$p];
		}

		return "";
	}
    public function getTrimParam($p, $method = null)
	{

		if (isset($_POST) && isset($_POST[$p])){
			return trim($_POST[$p]);
		}

		if (isset($_GET) && isset($_GET[$p])){
			return trim($_GET[$p]);
		}

		return "";
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl-upload-index',
		);
	}

    public function accessRules()
	{
		return array(
			array('allow',
					'actions'=>array('index','dir','posterAdd', 'posterEdit', 'posterDel', 'list','create','update', 'treelist','combolist','checkUnique', 'changePwd', 'remove', 'rename', 'files', 'add','dl'),
					'users'=>array('@'),
			),
			array('allow',
					'actions'=>array('contentList', 'stop', 'publish', 'listbygroupid', 'terminallist', 'save'),
					'users'=>array('@'),
			),
			//权限模块
			array('allow',
					'actions'=>array('delete', 'combolist', 'listbygroupid', 'terminallist', 'listbyid','deleteall','listbyroleid','exportdb','importdb'),
					'users'=>array('@'),
			),
			array('allow',
				'actions' => array("server",'getterminalupdate','deleteupdated','updateversion','devices', 'cityList', 'updateallWeather', 'updateWeather', 'listAll'),
				'users' => array('@')
			),
			array('allow',
				'actions' => array("validate", "exporttoexcel", "check", "index3", "getaudiobydir", "setserver", "submitserver"),
				'users' => array('@')
			),
			array('allow',
					'actions' => array('scheduler', 'info', 'preview2', 'setGroup', 'setting'),
					'users' => array('@')
			),
			array('allow',
					'actions'=>array('login', 'list','regist', 'logout', 'welcome', 'terminal', 'syncData', 'submitsync', 'uploadfile'),
					'users'=>array('*'),
			),
			array('allow',		//admin
					'actions' => array('Activate')
			),
			array('deny',  // deny all users
					'users'=>array('*'),
			)
		);
	}



	/**
	 *  字符串编码 to utf8，用于页面显示
	 * @param type $str
	 * @return type
	 */
	public  function encode($str)
	{
		@trigger_error('hi', E_USER_NOTICE);

		$ed = mb_detect_encoding($str, array('ASCII', 'GB2312','UTF-8'));	//GBK, cp939

		if($ed == "UTF-8"){
			return $str;
		}

		$r = @iconv("GB18030", "utf-8", $str);

		return $r;
	}

	/**
	 * 字符串解码, utf8 to gb，查获文件信息
	 * @param type $str
	 * @return type
	 */
	public  function decode($str)
	{
		@trigger_error('hi', E_USER_NOTICE);

		$ed = mb_detect_encoding($str, array('GB2312','UTF-8'));	//GBK, cp939,  EUC-CN

		if($ed == "GB2312" || $ed == "GB18030" || $ed == "EUC-CN" || $ed == "ASCII"){
			return $str;
		}
		$r = @iconv("utf-8", "GB18030", $str);

		return $r;
	}

	/**
	 * 返回当前日期时间 格式如：2012-02-12 12:00:00
	 */
	public function getDateTime()
	{
		return date("Y-m-d H:i:s");
	}


	/**
	 * 返回当前日期 格式： 2012-02-12
	 */
	public function getDate($time = null)
	{
		$time = ($time == null) ? time() : $time;
		return date("Y-m-d", $time);
	}
        public function getConfig($str)
    {
        $list = Config::model()->find("`Key`='".$str."'");
        return $list->Value;
    }
}